package com.maoslv.service.personal;

import com.maoslv.mapper.PersonalMapper;
import com.maoslv.pojo.Info;
import com.maoslv.pojo.Role;
import com.maoslv.pojo.User;
import com.maoslv.service.user.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PersonalServiceImpl implements PersonalService {
    @Autowired
    PersonalMapper personalMapper;

    @Autowired
    UserService userService;

    @Override
    public String getPass(Integer id) {
        return personalMapper.getPass(id);
    }

    @Override
    public int updatePass(Integer id, String newPass) {
        return personalMapper.updatePass(id,newPass);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public User getInfo(Integer infoId, Integer uid) {
        User userInfo = personalMapper.getUserInfo(infoId);
        Info info = personalMapper.getInfo(infoId);
        List<Role> userRole = userService.getUserRole(uid);
        userInfo.setRoleList(userRole);
        userInfo.setUserInfo(info);
        return userInfo;
    }

    @Override
    public Info getFormInfo(Integer id) {
        return personalMapper.getFormInfo(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateInfo(Info info) throws ParseException {
        Map<String, Object> map = new HashMap<>();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        map.put("id",info.getId());
        map.put("birthday",simpleDateFormat.parse(info.getBirthday()));
        map.put("city",info.getCity());
        map.put("home",info.getHome());
        Map<String, Object> userMap = new HashMap<>();
        userMap.put("id",info.getId());
        userMap.put("updateTime",new Date(System.currentTimeMillis()));
        personalMapper.updateUpdateTime(userMap);
        return personalMapper.updateInfo(map);
    }
}
